package xyz.funnygo.activity;

import xyz.funnygo.app.AppManager;
import xyz.funnygo.base.BaseActivity;
import xyz.funnygo.model.User;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.SaveListener;
import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends BaseActivity {

	@ViewInject(id = R.id.login_btn_submit, click = "login")
	private Button mButtonLogin;
	
	@ViewInject(id = R.id.login_quit, click = "quit")
	private ImageView mImageQuit;
	
	@ViewInject(id = R.id.login_username)
	private TextView mTextEmail;
	@ViewInject(id = R.id.login_password)
	private TextView mTextPassword;
	@ViewInject(id = R.id.login_txt_register, click = "goRegister")
	private TextView mTextReister;
	@ViewInject(id = R.id.login_txt_find_password, click = "goFindPassword")
	private TextView mTextFindPassword;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.avtivity_login);
	}

	public void login(View v) {
		final User user = new User();
		String name = mTextEmail.getText().toString();
		String pwd = mTextPassword.getText().toString();
		if(name.isEmpty()) {
			showToast("请输入用户名");
			return;
		}
		if(pwd.isEmpty()) {
			showToast("请输入用户名");
			return;
		}
		user.setUsername(name);
		user.setPassword(pwd);
		user.login(this, new SaveListener() {
			
			@Override
			public void onSuccess() {
				finish();
			}
			
			@Override
			public void onFailure(int code, String msg) {
				showToast("登录失败，"+msg);
			}
		});
		
	}

	public void goRegister(View v) {
		Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
		startActivity(intent);
	}
	
	public void goFindPassword(View v) {
		Intent intent = new Intent(LoginActivity.this, FindPasswordActivity.class);
		startActivity(intent);
	}
	
	public void quit(View v) {
		AppManager.getInstance().finishAllActivity();
		finish();
	}
	
}
